<?php
namespace app\home\controller;

use app\common\component\user\UserService;
use app\common\component\user_sign\UserSignModel;
use app\common\component\user_sign\UserSignService;

class Index extends ControllerBase {
    public function index() {
    	$getUserSign = (new UserSignService()) -> getUserSign();

		$today = strtotime(date('Ymd'));

    	if($getUserSign['code'] === 0) {
    		// 判断今天是否已经签到
    		$getUserSign['data']['user_sign'] -> is_sign = $getUserSign['data']['user_sign'] -> last_time == $today;
    		$this -> view -> assign('userSign', $getUserSign['data']['user_sign']);
    	}

		$userSignModel = new UserSignModel();
    	$userSignStarUsername = cache('userSignStarUsername');

		if(!$userSignStarUsername) {
    		$getUserSignStar = $userSignModel -> order('month_total_day DESC') -> find();
			if($getUserSignStar) {
	    		$userSignStar = (new UserService) -> getUser($getUserSignStar -> user_id);
	    		if($userSignStar['code'] == 0) {
					$userSignStarUsername = $userSignStar['data']['user'] -> username;

					cache('userSignStarUsername', $userSignStarUsername, 86400);
	    		}
			}
		}
    	$this -> view -> assign('userSignStarUsername', $userSignStarUsername);

		$page = 10;
		// 当天签到排行
		$cTotalDays = $userSignModel -> where('last_time = ' . strtotime(date('Ymd'))) -> order('continuity_total_day DESC') -> paginate($page, true);
		$this -> view -> assign('cTotalDays', $cTotalDays);

		// 当月签到排行
		$mTotalDays = $userSignModel -> order('month_total_day DESC') -> paginate($page, true);
		$this -> view -> assign('mTotalDays', $mTotalDays);

		// 总签到排行
		$totalDays = $userSignModel -> order('total_day DESC') -> paginate($page, true);
		$this -> view -> assign('totalDays', $totalDays);

		// 总奖金到排行
		$totalPoints = $userSignModel -> order('total_points DESC') -> paginate($page, true);
		$this -> view -> assign('totalPoints', $totalPoints);

    	return $this -> view -> fetch();
    }
}